$(document).ready(function() {
        $('#button,button').button();
        $('#selectables li').click(function(){
                if ($(this).hasClass("ui-selected"))
                {
                    $(this).removeClass("ui-selected");
                }
                else {
                    $(this).addClass("ui-selected");
                }
                $('#customlist').dataTable().fnDraw();
            });
        //ratiobar
        $.each($('.ratiobar'),function(){
                ratio=parseInt($(this).text());
                $(this).empty();
                $(this).progressbar({
                    value: ratio
                            });
            });

        //**ranking bars**
        max_value=-99999;
        min_value=99999;
        $.each($('.mark'),function(){
                value = parseFloat($('.rankingbar',this).text());
                if (value > max_value){ max_value=value };
                if (value < min_value){ min_value=value };
            });
        //we floor the values
        max_value = max_value - min_value;
        
        $.each($('.mark'),function(){
                value = parseFloat($('.rankingbar',this).text());
                $('.rankingbar',this).empty();
                final_val = (value - min_value) * 100 / max_value;
                $('.rankingbar',this).progressbar({value : final_val});
            });
        $('.rankingbar').css({"width":"30px","height":"20px","margin":"2px"});
        //$('.ui-progressbar-value').css({"background-image":"url(/images/rankingbar.png)"});

        //*end ranking bars*

        //*datatables*
        $.fn.dataTableExt.afnFiltering.push(
            function( oSettings, aData, iDataIndex ) {
                id=aData[0];
                if (isNaN(parseInt(id))) { return true;}
                s = $('#selectables li#'+id);
                if (!s.length) {return true;};
                if (s.hasClass("ui-selected")){
                    return true;
                }
                return false;
                
            });

        $('#listtable').dataTable({ 
                "bJQueryUI": true,
                    "sPaginationType": "full_numbers",
                    "bPaginate": false,
                    "aoColumnDefs": [ { "sType": "numeric", "aTargets": [ 0 ] },
                                      { "sClass": "center-text", "aTargets": [ 0 ] },
                                      { "bSortable": false, "aTargets": [ 0 ] } ]
                    });
      
        $('#rankingtable').dataTable({ 
                "bJQueryUI": true,
                    "sPaginationType": "full_numbers",
                    "bPaginate": false,
                    "sScrollX": "100%",
                    "aoColumnDefs": [ { "sType": "numeric", "aTargets": [ 0,1 ] },
                                      { "sClass": "center-text", "aTargets": [ 0,1 ] },
                                      { "bSortable": false, "aTargets": [ 1 ] } ]
                    });

        $('#customlist').dataTable({
                "bJQueryUI": true,
                    "sPaginationType": "full_numbers",
                    "bPaginate": false,
                    "sScrollX": "100%",
                    "aoColumnDefs": [ { "sType": "numeric", "aTargets": [ 0 ] },
                                      { "sClass": "center-text", "aTargets": [ 0 ] } ]
                    });
        

      
        //remove login button in login window
        if (window.location.pathname == "/login"){
            $('#logout').empty();
            $('#logout').css("background","none");
        }
      
        $.getJSON('/message',function(data){
                $('div#messages').empty();
                if (data.length > 0){
                    $.each(data,function(){
                            message = $('<div class="message"></div>');
      
                            switch (this.type){
                                case "error":
                                    $(message).addClass("ui-state-error");
                                    $(message).html('<span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;">');
                                    break;
                                case "info":
                                    $(message).addClass("ui-state-highlight");
                                    $(message).html('<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;">');
                                    break;
                            }
                            $(message).append(this.message);
                            $(message).append("</span>");
                            $('div#messages').append(message);
                        });
                    $('.message').css({'margin':'10px auto','padding':'5px','width':'250px'});
                    setTimeout(function(){$("div#messages").fadeOut();}, 1000*5);
                }
            });    
    });